<template>
  <el-table
    :data="userList"
    style="width: 100%"
    :border="true"
  >
    <el-table-column type="expand">
      <template slot-scope="{row}">
        <el-row v-for="item in row.children" :key="item.id" class="bdtottom bdtop bdright">
          <!-- 第一层数据 -->
          <el-col :span="8" class="h-center">
            <el-tag closable effect="dark" @close="onClose(item.id,row.id,row)">
              {{ item.authName }}
            </el-tag>
            <i class="el-icon-caret-right" />
          </el-col>
          <!-- 第二层以及第三层数据 -->
          <el-col :span="16">
            <el-row v-for="item2 in item.children" :key="item2.id">
              <el-col :span="6">
                <el-tag type="success" closable effect="dark" @close="onClose(item2.id,row.id,row)">
                  {{ item2.authName }}
                </el-tag>
                <i class="el-icon-caret-right" />
              </el-col>
              <el-col :span="18">
                <el-tag v-for="item3 in item2.children" :key="item3.id" type="warning" closable effect="dark" @close="onClose(item3.id,row.id,row)">
                  {{ item3.authName }}
                </el-tag>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </template>

    </el-table-column>
    <el-table-column
      label="#"
      type="index"
    />
    <el-table-column
      label="角色名称"
      prop="roleName"
    />
    <el-table-column
      label="角色描述"
      prop="roleDesc"
    />
    <el-table-column
      label="操作"
    >
      <template slot-scope="{row}">
        <user-button @distribution="$emit('OnDistribution',row.id)" @delete="$emit('OnDelete',row.id)" @edit="$emit('onEdit',row)" />
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import UserButton from './UserButton.vue'
import { getRolesList, DeleteRolesPower } from '@/api/role.js'
export default {
  components: { UserButton },
  data() {
    return {
      userList: []
    }
  },

  created() {
    this.loadRoleList()
  },

  methods: {
    async loadRoleList() {
      const res = await getRolesList()
      this.userList = res.data
      console.log(this.userList)
      this.$emit('userList', this.userList)
    },
    onClose(val, id, e) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await DeleteRolesPower(id, val)
        e.children = res.data
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
 .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
  .el-table {
    margin-top: 15px;
  }
  .el-tag{
    margin: 7px;
  }

.el-row{
  border-left: 1px solid #efefef;
  display: flex;
  align-items: center;
}
.bdtottom{
  border-bottom: 1px solid #efefef;
}
.bdtop{
  border-top: 1px solid #efefef;
}
.bdright{
      border-right: 1px solid #efefef;
}
.h-center{
      display: flex;
    justify-content: center;
    align-items: center;
}
</style>
